package com.webull.information.center.carwler.common.util.jsoup;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import org.apache.commons.lang3.RandomStringUtils;
import org.jsoup.Connection;

/**
 * 
 * @author smj
 * @Date 2017年7月7日上午10:08:14
 * @version 1.0
 * 
 *          JsoupModel 返回connection的head模式
 */
public enum JsoupModel {

	Mobile0, // 模拟手机浏览器(华尔街日报，header)
	Mobile1, // 模拟手机浏览器(黄金日报，header)
	PC0;// 模拟pc浏览器

	public Connection adderModel(Connection connection) {
		Function<Connection, Connection> position = cachedPosition.get(this);
		if (position != null) {
			return position.apply(connection);
		}
		throw new IllegalArgumentException(this + "ModelPosition can't be null.");
	}
	/*
	 * 
	 */
	private static Map<JsoupModel, Function<Connection, Connection>> cachedPosition = new ConcurrentHashMap<>();
	static {
		cachedPosition.put(Mobile0, ModelPosition::mobile0Model);
		cachedPosition.put(PC0, ModelPosition::pC0Model);
		cachedPosition.put(Mobile1, ModelPosition::mobile1Model);
	}
	
	private static class ModelPosition {

		private static Connection pC0Model(Connection connection) {
			connection.ignoreContentType(true).userAgent(
					"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
					.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
			return connection;
		}

		/*
		 * iphone6plus -->mock mobile
		 */
		private static Connection mobile0Model(Connection connection) {
			connection.ignoreContentType(true).userAgent(
					"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
					.header("cache-control", "no-cache")
					.header("x-ivanka-platform", "wscn-platform")
			;
			return connection;
		}

		/*
		 * iphone6plus -->mock mobile
		 */
		private static Connection mobile1Model(Connection connection) {
			connection.ignoreContentType(true).userAgent(
					"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
					.header("cache-control", "no-cache").header("x-ivanka-platform", "gold-platform");
			return connection;
		}
	}

}
